package com.gxa.controller;

import com.gxa.pojo.Store;
import com.gxa.service.StoreService;
import com.gxa.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/store")
@CrossOrigin
public class StoreController {
    @Autowired
    StoreService storeService;

    JsonResult jsonResult=new JsonResult();

    @ResponseBody
    @RequestMapping(value = "/storePage")
    public Map findStoreByPage(@RequestBody Map map){
        String page=map.get("number").toString();
        String size=map.get("size").toString();
        System.out.println("page:"+page);
        System.out.println("size:"+size);
        Integer offset=(Integer.parseInt(page)-1)*Integer.parseInt(size);
        Integer limit=Integer.parseInt(size);
        List<Store> stores = storeService.findStoreByPage(offset, limit);
        Integer count=storeService.findAllStore();
        jsonResult.setData(stores);
        jsonResult.put("count",count);
        return jsonResult.getValues();
    }

    @ResponseBody
    @RequestMapping("/findAll")
    public List<Store> findAll(){
        List<Store> all = storeService.findAll();
        return all;
    }

    @ResponseBody
    @RequestMapping("/storeEdit")
    public Map updateStore(@RequestBody Map map){
        jsonResult=new JsonResult();
        Integer n=storeService.updateStore(map);
        if (n>0){
            jsonResult.setCode("200");
            jsonResult.setMsg("修改成功");
        }else {
            jsonResult.setCode("406");
            jsonResult.setMsg("系统开了小差！！");
        }
        return jsonResult.getValues();
    }

    @ResponseBody
    @RequestMapping("/storeAdd")
    public Map insertStore(@RequestBody Map map){
        jsonResult=new JsonResult();
        String name = map.get("name").toString();
        Store storeByName = storeService.findStoreByName(name);
        if (storeByName!=null){
            jsonResult.setCode("401");
            jsonResult.setMsg("仓库名重复！");
            return jsonResult.getValues();
        }
        System.out.println(map);
        Integer n=storeService.insertStore(map);
        if (n>0){
            jsonResult.setCode("200");
            jsonResult.setMsg("添加成功");
        }else {
            jsonResult.setCode("406");
            jsonResult.setMsg("系统开了小差！！");
        }
        return jsonResult.getValues();
    }

    @ResponseBody
    @RequestMapping("/storeDel")
    public Map deleteStoreById(@RequestBody Map map){
        jsonResult=new JsonResult();
        String storeID=map.get("storeID").toString();
        Integer n=storeService.deleteStoreById(Integer.valueOf(storeID));
        if (n>0){
            jsonResult.setCode("200");
            jsonResult.setMsg("删除成功");
        }else {
            jsonResult.setCode("406");
            jsonResult.setMsg("系统开了小差！！");
        }
        return jsonResult.getValues();
    }

    @ResponseBody
    @RequestMapping("/storeSeach")
    public Map selectStoreByNameAndManagerName(@RequestBody Map map){
        jsonResult=new JsonResult();
        System.out.println(map);
        Integer count = storeService.findAllStore2(map);
        System.out.println(count);
        String number=map.get("number").toString();
        String size = map.get("size").toString();
        Integer offset = (Integer.parseInt(number) - 1) * Integer.parseInt(size);
        Integer limit= Integer.valueOf(size);
        map.put("offset",offset);
        map.put("limit",limit);
        System.out.println(map);
        List<Store> stores = storeService.selectStoreByNameAndManagerName(map);
        System.out.println(stores);
        if (stores!=null){
            jsonResult.setData(stores);
            jsonResult.put("count",count);
            jsonResult.setCode("200");
            jsonResult.setMsg("查询成功");
        }else {
            jsonResult.setCode("406");
            jsonResult.setMsg("系统开了小差！！");
        }
        return jsonResult.getValues();
    }
    
    @ResponseBody
    @RequestMapping("/storeState")
    public Map updateState(@RequestBody Map map){
        jsonResult=new JsonResult();
        Integer n = storeService.updateState(map);
        if (n>0){
            jsonResult.setCode("200");
            jsonResult.setMsg("状态修改成功");
        }else {
            jsonResult.setCode("406");
            jsonResult.setMsg("系统开了小差！！");
        }
        return jsonResult.getValues();
    }
}